package calendar;

import edu.gmu.middleware.Message;
import edu.gmu.streams.BlockInputStream;
import edu.gmu.streams.BlockOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

/* this class represents a calendar event */
public class Event implements Message {

  private static final long serialVersionUID = 348575223L;

  User owner;
  int id;
  Date start;
  Date end;
  boolean isSent;
  int numSent;
  int type;
  String desc;
  ArrayList users;

  public Event()
  {
    this.owner = null;
    this.id = 0;
    this.start = null;
    this.end = null;
    this.isSent = false;
    this.numSent = 1;
    this.type = 0;
    this.desc = null;
    this.users = null;
  }

  public Event( int type, String desc, Date start, Date end, User owner )
  {
    Random r   = new Random();
    this.id    = r.nextInt();
    this.owner = owner;
    this.start = new Date( start.getTime() );
    this.end   = new Date( end.getTime() );
    this.isSent = false;
    this.numSent = 1;
    this.type  = type;
    this.desc  = desc;
    this.users = new ArrayList();
  }

  public int getId()
  {
    return this.id;
  }

  public User getOwner()
  {
    return this.owner;
  }

  public void setOwner( User owner )
  {
    this.owner = owner;
  }

  public Date getStart()
  {
    return new Date( this.start.getTime() );
  }

  public Date getEnd()
  {
    return new Date( this.end.getTime() );
  }

  public boolean getIsSent()
  {
    return isSent;
  }

  public void setIsSent()
  {
    if ( numSent >= users.size() ) this.isSent = true;
    else numSent++;
  }

  public int getType()
  {
    return this.type;
  }

  public String getDesc()
  {
    return this.desc;
  }

  public void setStart( Date start )
  {
    this.start = new Date( start.getTime() );
  }

  public void setEnd( Date end )
  {
    this.end = new Date( end.getTime() );
  }

  public void setDesc( String desc )
  {
    this.desc = desc;
  }

  public void setUsers( ArrayList users )
  {
    this.users = users;
  }

  public void removeUser ( User user )
  {
    this.users.remove( user );
  }

  public void addUser ( User user )
  {
    this.users.add( user );
  }

  public ArrayList getUsers()
  {
    return users;
  }

  /* check the user id to see if he is a participant of this event */
  public boolean isParticipant( User user )
  {
      for (int i = 0; i < users.size(); i++)
      {
          User aUser = (User)users.get(i);
          if( aUser.equals(user) )
          {
              return true;
          }
      }

    return false;
  }

  public void marshal(BlockOutputStream stream) throws IOException
  {
    this.owner.marshal( stream );
    stream.writeInt( id );
    stream.writeLong( start.getTime() );
    stream.writeLong( end.getTime() );
    stream.writeBoolean( isSent );
    stream.writeInt( numSent );
    stream.writeInt( type );
    stream.writeUTF( desc );
    stream.writeInt( users.size() );
    for ( int i = 0; i < users.size(); i++ )
    {
        User aUser = (User)this.users.get(i);
        aUser.marshal( stream );
    }
  }

  public void unmarshal(BlockInputStream stream) throws IOException
  {
    this.owner = new User();
    this.owner.unmarshal( stream );
    
    this.id = stream.readInt();
    
    this.start = new Date( stream.readLong() );
    this.end = new Date( stream.readLong() );
    this.isSent = stream.readBoolean();
    this.numSent = stream.readInt();
    this.type = stream.readInt();
    this.desc = stream.readUTF();
    int tempSize = stream.readInt();
    this.users = new ArrayList();
    for( int i = 0; i < tempSize; i++){
      User aUser = new User();
      aUser.unmarshal(stream);
      this.users.add(aUser);
    }
  }
}
